package cn.yusei.teamTribe.controller.team;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.yusei.teamTribe.constant.MessageType;
import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.constant.RoleType;
import cn.yusei.teamTribe.entity.TeamMemberRequest;
import cn.yusei.teamTribe.service.MessageService;
import cn.yusei.teamTribe.service.team.TeamMemberService;
import cn.yusei.teamTribe.service.team.TeamRoleService;
import cn.yusei.teamTribe.util.ResponseUtil;

@Controller
public class TeamMemberController {

	@Autowired
	private TeamMemberService tmService;
	@Autowired
	private MessageService messageService;
	@Autowired
	private TeamRoleService trService;
	@Value("${teamRequestReasonLength}")
	private int teamRequestReasonLength;
	@Value("${teamResponseReasonLength}")
	private int teamResponseReasonLength;
	
	@RequestMapping(value="/teamMember/sendRequest")
	public void sendRequest(
			@RequestAttribute int id,
			@RequestParam int teamId,
			@RequestParam(value="reason", defaultValue="") String reason,
			PrintWriter out) {
		if(teamId <= 0 || reason.length() > teamRequestReasonLength) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int code = 0;
		int requestId = 0;
		try {
			code = tmService.addRequest(id, teamId, reason);
			if(code != 0) {
				ResponseUtil.responseCode(out, code);
				return;
			}
			requestId = tmService.selectLastInsertId();
			//分别向 teamId 会长和所有管理员发送消息
			for(int userId : trService.get(teamId, RoleType.TEAM_MASTER)) {
				messageService.addMessage(userId, MessageType.TEAM_MEMBER_REQUEST, requestId);
			}
			for(int userId : trService.get(teamId, RoleType.TEAM_MANAGER)) {
				messageService.addMessage(userId, MessageType.TEAM_MEMBER_REQUEST, requestId);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", requestId);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}
	
	@RequestMapping(value="/teamMember/getById")
	public void getRequest(@RequestParam int id, PrintWriter out) {
		if(id <= 0) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		TeamMemberRequest tmr = null;
		try {
			tmr = tmService.getRequest(id);
		}
		catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("request", tmr);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}
	
	@RequestMapping(value="/teamMember/sendResponse")
	public void sendResponse(
			@RequestAttribute int id,
			@RequestParam int requestId,
			@RequestParam int result,
			@RequestParam(value="reason", defaultValue="") String reason,
			PrintWriter out) {
		if(requestId <= 0 || reason.length() > teamResponseReasonLength) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		if(result !=1 && result != 2){
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int code = 0;
		try {
			code = tmService.setResponse(id, requestId, result, reason);
			if(code != 0) {
				ResponseUtil.responseCode(out, code);
				return;
			}
			int userId = tmService.getRequestUserId(requestId);
			messageService.addMessage(userId, MessageType.TEAM_MEMBER_RESPONSE, requestId);
		}
		catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		ResponseUtil.responseCode(out, ResponseCode.REQUEST_SUCCESSED);
	}
	
	@RequestMapping(value="/teamMember/get")
	public void get(@RequestParam int teamId, @RequestParam int offset, @RequestParam int num, PrintWriter out) {
		if(teamId <= 0 || offset < 0 || num <=0) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int[] ids = null;
		try {
			ids = tmService.getMembers(teamId, offset, num);
		}
		catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", ids);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}
	
	@RequestMapping(value="/teamMember/delete")
	public void delete(@RequestAttribute int id, @RequestParam int targetId, @RequestParam int teamId, PrintWriter out) {
		if(targetId <= 0 || teamId <= 0) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int code = 0;
		try {
			code = tmService.delete(id, targetId, teamId);
		}
		catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		ResponseUtil.responseCode(out, code);
	}
}
